package com.exam.controller;


import com.exam.entity.*;
import com.exam.entity.common.ResponseResult;
import com.exam.service.*;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class SubjectController {


    @Autowired
    @Qualifier("subjectServiceImpl")
    private SubjectService subjectService;
    @Autowired
    @Qualifier("shortAnswerQServiceImpl")
    private ShortAnswerQService shortAnswerQService;
    @Autowired
    @Qualifier("singleChoiceQServiceImpl")
    private SingleChoiceQService singleChoiceQService;
    @Autowired
    @Qualifier("judgmentQServiceImpl")
    private JudgmentQService judgmentQService;
    @Autowired
    @Qualifier("fillBlanksQServiceImpl")
    private FillBlanksQService fillBlanksQService;

    ResponseResult responseResult=new ResponseResult();
    Subject subject=new Subject();
    @RequestMapping("/subject_list")
    public String subject(){
        return "views/manage/subject_list";
    }

    @RequestMapping("/get_all_subject")
    @ResponseBody
    public String get_all_subject(){
        List<Subject> all_subject = subjectService.get_all_subject();
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(all_subject);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/del_subject_by_s_id")
    @ResponseBody
    public String del_subject_by_s_id(int s_id){
        int i = subjectService.del_subject_by_s_id(s_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/add_subject")
    @ResponseBody
    public String add_subject(String s_name){
        subject.setS_creat_time(TimeUtils.getTime());
        subject.setS_name(s_name);
        subject.setS_state(1);
        int i = subjectService.add_subject(subject);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_subject_question_library_by_s_id")
    public String get_subject_question_by_s_id(int s_id, Model model){
        model.addAttribute("s_id",s_id);
        return "views/manage/subject_question_library";
    }


    @RequestMapping("/get_subject_question_total_by_s_id_and_level")
    @ResponseBody
    public String get_subject_question_total_by_s_id_and_level(int s_id,String question_level){
        //单选题

        Map<String,Object> map=new HashMap<>();
        map.put("s_id",s_id);
        map.put("question_level",question_level);

        Map<String,Object> resMap=new HashMap<>();

        List<SingleChoiceQuestions> singleChoiceQ = singleChoiceQService.get_question_by_s_id_and_level(map);
        List<JudgmentQuestions> judgmentQ = judgmentQService.get_question_by_s_id_and_level(map);
        List<ShortAnswerQuestions> shortAnswerQ = shortAnswerQService.get_question_by_s_id_and_level(map);
        List<FillBlanksQuestions> fillBlanksQ = fillBlanksQService.get_question_by_s_id_and_level(map);
        resMap.put("singleChoiceQ",singleChoiceQ);
        resMap.put("judgmentQ",judgmentQ);
        resMap.put("shortAnswerQ",shortAnswerQ);
        resMap.put("fillBlanksQ",fillBlanksQ);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(resMap);
        return JsonUtils.getJson(responseResult);
    }

}
